package com.sslite.standalone;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.sslite.dao.MetaDataDao;
import com.sslite.model.MetaData;
import com.sslite.reports.ReportController;
import com.sslite.util.SSLiteProperties;

public class DailyRun {
	public static void main(String... args) throws Exception{
		// Step 1: Pull to-be-processed Symbols and their MetaData
		final List<String> symbols = Arrays.asList( SSLiteProperties.SYMBOLS.getValue().replaceAll(" ", "").split("[,]") );
		
		final List<MetaData> metaDataList = MetaDataDao.getMetaDataFor( symbols );

		System.out.println( "DailyRun: Starting to process a total of " +metaDataList.size()+ " symbols." );
		
		final List<String> exceptionInSymbols = new ArrayList<String>();
		final List<String> exceptionOneMinuteSymbols = new ArrayList<String>();
		
		// Step 2: Download IntraDay data from Google and append it to one.minute file
		if( SSLiteProperties.GENERATE_REPORT_ONLY.getValue().equalsIgnoreCase("false") ){
			IntraDayDownloader.downloadIntraDayDataFromGoogle(metaDataList, exceptionInSymbols, exceptionOneMinuteSymbols);
		}
		
		// Step 3: Generate Report
		new ReportController().generateReports(metaDataList);
		
		if( exceptionInSymbols != null && !exceptionInSymbols.isEmpty() ){
			System.out.println( "\nDailyRun: Could not process " +exceptionInSymbols.size()+ " symbols: " +exceptionInSymbols );
		}
		if( exceptionOneMinuteSymbols != null && !exceptionOneMinuteSymbols.isEmpty() ){
			System.out.println( "\nDailyRun: Could not save one.minute data for : " +exceptionOneMinuteSymbols+". To resolve delete <SYMBOL>-INTRA-DAY file from cache.folder, remove any zero-volume entries towards the end of one.minute file for these symbols and rerun the process for these symbols with generate.report.only=false. \nCaution: Keep a copy of generated report otherwise it will overwrite it and you will have to re-run report again for all symbols." );
		}
		
		System.out.println( "\n\nDailyRun: Note: Run GoogleStockScreener every 2 weeks to include new qualifying symbols." );
		System.out.println( "DailyRun: Note: Run MetaDataDao every 1 month to update SharesOutstanding information." );
		System.out.println( "DailyRun: Done." );
		System.exit(0);
	}
	
}
